package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.vo.request.*;
import com.zhijian.medical.entity.vo.response.*;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.*;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考核主表 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2023-01-17 16:40:22
 */
@Slf4j
@Controller
@Api(tags = "考核管理【新】")
public class AssessmentController {

    @Autowired
    private AssessmentService service;

    @Autowired
    private PersonService personService;

    @Autowired
    private DigitalSliceCaseService digitalSliceCaseService;

    @Autowired
    private AssessmentPaperService assessmentPaperService;

    @Autowired
    private AssessmentStudyService assessmentStudyService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/assessment")
    @ApiOperation("新增")
    public boolean insert(@RequestBody AssessmentSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/assessment/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/assessment/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody AssessmentSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/assessment/{id}")
    @ApiOperation("根据id查询")
    public AssessmentVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/assessment/page")
    @ApiOperation("分页查询")
    public Page<AssessmentVo> selectByCondition(@RequestBody(required = false) AssessmentSearchVo param) {
        if (param == null)
            param = AssessmentSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }


    /**
     * 人员列表，选择做卷人与阅卷人用
     *
     * @return
     */
    @Response
    @GetMapping("/person/list")
    @ApiOperation("人员列表，选择做卷人与阅卷人用")
    public List<PersonVo> selectPersonList() {
        return personService.selectList(PersonSearchVo.builder().build());
    }

    /**
     * 根据类型查询数字切片数据
     *
     * @param type
     * @return
     */
    @Response
    @GetMapping("/digital/slice/case/list")
    @ApiOperation("根据类型查询数字切片数据")
    public List<DigitalSliceCaseVo> selectCaseListByType(@ApiParam(name = "type", value = "类型", required = false) @RequestParam(value = "type", required = false) String type) {
        return digitalSliceCaseService.selectListOnly(DigitalSliceCaseSearchVo.builder()
                .type(type)
                .build());
    }

    /**
     * 考核详情
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/assessment/paper/main/detail")
    @ApiOperation("考核详情")
    public AssessmentWebDetailVo mainDetail(@RequestBody AssessmentPaperPersonSearchVo param) {
        return assessmentPaperService.mainDetail(param);
    }


    /**
     * 阅卷人打分提交
     *
     * @param assessmentPaperDoScoreVo
     * @return
     */
    @Response
    @PostMapping("/assessment/paper/score")
    @ApiOperation("阅卷人打分提交")
    public boolean score(@RequestBody AssessmentPaperDoScoreVo assessmentPaperDoScoreVo) {
        assessmentPaperDoScoreVo.setReviewerId(CurrentUserHolder.getUser().getId());
        return assessmentPaperService.score(assessmentPaperDoScoreVo);
    }

    /**
     * 根据考核id与做卷人id查询出此做卷人，填写的数字切片部分的答案
     *
     * @param assessmentId
     * @param doUserId
     * @return
     */
    @Response
    @GetMapping("/assessment/paper/answer/dsc/{assessmentId}/{doUserId}")
    @ApiOperation("根据考核id与做卷人id查询出此做卷人，填写的数字切片部分的答案")
    public List<AssessmentAnswerOfDigitalSliceCaseVo> selectAssessmentAnswerOfDigitalSliceCaseListByCondition(@ApiParam(name = "assessmentId", value = "考核id") @PathVariable("assessmentId") Long assessmentId,
                                                                                                              @ApiParam(name = "doUserId", value = "做卷人id") @PathVariable("doUserId") Long doUserId) {
        return assessmentPaperService.selectAssessmentAnswerOfDigitalSliceCaseListByCondition(assessmentId, doUserId);
    }

    /**
     * 根据学习模块id查询关联考核列表
     *
     * @param studyId
     * @return
     */
    @Response
    @GetMapping("/assessment/list/{studyId}")
    @ApiOperation("根据学习模块id查询关联考核列表")
    public List<AssessmentListVo> selectAssessmentListByStudyId(@ApiParam(name = "studyId", value = "学习模块id") @PathVariable("studyId") Long studyId) {
        return assessmentStudyService.selectAssessmentListByStudyId(studyId);
    }

}